from manim import *



class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
         #题目出现
        text = Tex(r"\text{设}$\left\{ a_{n}\right\} ,\left\{ b_{n}\right\} $\text{均为正项数列,其中}$\left\{ a_{n}\right\} $\text{无界,}$\left\{ b_{n}\right\} $\text{有界,则下列数列中一定有界的是()}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0)
        text1 = Tex(
                   r"\text{(A)}$\left\{ \dfrac{b_{n}}{a_{n}}\right\} $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-32,-5,0])
        text2 = Tex(
                    r"\text{(B)}$\left\{ \dfrac{b_{n}}{1+a_{n}b_{n}}\right\} $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-12,-5,0])
        text3 = Tex(
                   r"\text{(C)}$\sqrt[n] {a_{n}b_{n}}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([12,-5,0])
        text4 = Tex(
                    r"\text{(D)}$a_{n}b_{n}-a_{n-1}b_{n-1}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([32,-5,0])

        self.play(Write(text),self.camera.frame.animate.scale(0.8),Write(text1),Write(text2),Write(text3),Write(text4))
        self.wait(0.1)
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text.animate().move_to([0,20,0]),text1.animate().move_to([-32,15,0]) ,text2.animate().move_to([-12,15,0]),text3.animate().move_to([12,15,0]),text4.animate().move_to([32,15,0]) )
        self.wait(0.1)

         #半透明矩阵扩展
        rectangle=Rectangle(color=BLUE,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-5,20,0])
        self.play(
            rectangle.animate.stretch(10.0,dim=0)
        )
        dot = Dot([-30, -15, 0])
        dot2 = Dot([-30, -25, 0])
        line = Line(dot, dot2).set_color(ORANGE)
        text5 = Tex(
                    r"\text{反例：}$a_{n}=$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-40,-20,0])
        
        b2 = Brace(line, direction=line.copy().rotate(-PI / 2).get_unit_vector(),stroke_width=20,sharpness=3)
        self.play(Write(text5))
        self.add(b2)
        text6 = Tex(
                    r"$n$  \text{   ,  n为奇}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-22,-15,0])
        text7 = Tex(
                    r"$\dfrac{1}{n}$   \text{   ,  n为偶}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-22,-25,0])
        self.play(Write(text6),Write(text7))

        #半透明矩阵扩展
        rectangle1=Rectangle(color=YELLOW,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([7,20,0])
        self.play(
            rectangle1.animate.stretch(10.0,dim=0)
        )
        text8 = Tex(
                    r"$b_{n}=1$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([7,25,0])
        self.play(Write(text8))

        
        #A改变
        dot3 = Dot([-34, 5, 0])
        dot4 = Dot([-34, -5, 0])
        line1 = Line(dot3, dot4).set_color(ORANGE)
        text9 = Tex(
                    r"$(A)\dfrac{b_{n}}{a_{n}}=$",color=RED_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-40,0,0])
        
        b3 = Brace(line1, direction=line1.copy().rotate(-PI / 2).get_unit_vector(),stroke_width=20,sharpness=3,color=RED_C)
        text10 = Tex(
                    r"$\dfrac{1}{n}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-32,5,0])
        text11 = Tex(
                    r"$n$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-32,-5,0])
        #B改变
        dot5 = Dot([-11, 5, 0])
        dot6 = Dot([-11, -5, 0])
        line2 = Line(dot5, dot6).set_color(ORANGE)
        text12 = Tex(
                    r"$(B)\dfrac{b_{n}}{1+a_{n}}=$",color=RED_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,0,0])
        
        b4 = Brace(line2, direction=line1.copy().rotate(-PI / 2).get_unit_vector(),stroke_width=20,sharpness=3,color=RED_C)
        text13 = Tex(
                    r"$\dfrac{1}{1+n}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-7,5,0])
        text14 = Tex(
                    r"$\dfrac{1}{1+\dfrac{1}{n}}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-7,-5,0])
        
        #C改变

        dot7 = Dot([16, 5, 0])
        dot8 = Dot([16, -5, 0])
        line3 = Line(dot7, dot8).set_color(ORANGE)
        text15 = Tex(
                    r"$(C)\sqrt[n] {a_{n}b_{n}}=$",color=RED_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([8,0,0])
        
        b5 = Brace(line3, direction=line1.copy().rotate(-PI / 2).get_unit_vector(),stroke_width=20,sharpness=3,color=RED_C)
        text16 = Tex(
                    r"$\sqrt[n] {n}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([19,5,0])
        text17 = Tex(
                    r"$\dfrac{1}{\sqrt[n] {n}}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([19,-5,0])
        #D改变

        dot9 = Dot([26, -10, 0])
        dot10 = Dot([26, -20, 0])
        line4 = Line(dot9, dot10).set_color(ORANGE)
        text18 = Tex(
                    r"$(D)a_{n}b_{n}-a_{n-1}b_{n-1}=$",color=RED_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([9,-15,0])
        
        b6 = Brace(line4, direction=line1.copy().rotate(-PI / 2).get_unit_vector(),stroke_width=20,sharpness=3,color=RED_C)
        text19 = Tex(
                    r"$n-\dfrac{1}{n-1}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([33,-10,0])
        text20 = Tex(
                    r"$\dfrac{1}{n}-\left( n-1\right) $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([33,-20,0])
        self.play(Write(text12),Write(text9),Write(text15),Write(text18))
        self.add(b3,b4,b5,b6)
        self.play(Write(text13),Write(text14),Write(text11),Write(text10),Write(text16),Write(text17),Write(text19),Write(text20))

        #框框标记
        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=15,height=15
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle2.move_to([-38, 0, 0])
        
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=45,height=15
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle1.move_to([20, -15, 0])
        self.play(Create(rectangle1),Create(rectangle2))

        #框框移动+AC消失
        self.play(rectangle2.animate().move_to([15,0,0]).stretch(2.0,dim=0),
                  FadeOut(rectangle1),FadeOut(text9),FadeOut(text10),FadeOut(text11),FadeOut(b3)
                  ,FadeOut(text18),FadeOut(text19),FadeOut(text20),FadeOut(b6)
        )

        #箭头+文本
        arrow = Arrow(start=[21,5,0],end=[25,5,0],color=GREEN
                       , stroke_width=20, tip_length=1)
        arrow1 = Arrow(start=[21,-5,0],end=[25,-5,0],color=GREEN
                       , stroke_width=20, tip_length=1)
        self.play(GrowArrow(arrow),GrowArrow(arrow1))
        text21 = Tex(
                    r"$1$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([27,5,0])
        text22 = Tex(
                    r"$1$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([27,-5,0])
        self.play(Create(text21),Create(text22))
        #框框移动
        self.play(rectangle2.animate().move_to([-15,0,0]).stretch(1.0,dim=0).stretch(1.2,dim=1)
        )
        #框框移动+B消失
        self.play(rectangle2.animate().move_to([15,0,0]).stretch(1.0,dim=0).stretch(1.0,dim=1)
                  ,FadeOut(text12),FadeOut(text13),FadeOut(text14),FadeOut(b4)
        )
        #反例加强
        #箭头
        arrow3 = Arrow(start=[-10,-20,0],end=[0,-20,0],color=RED
                       , stroke_width=20, tip_length=1)
        text220 = Tex(
                    r"\text{强化}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-5,-15,0])

        self.play(GrowArrow(arrow3),Write(text220))

        dot = Dot([13, -15, 0])
        dot2 = Dot([13, -25, 0])
        line = Line(dot, dot2).set_color(ORANGE)
        text5 = Tex(
                    r"$a_{n}=$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([8,-20,0])
        
        b2 = Brace(line, direction=line.copy().rotate(-PI / 2).get_unit_vector(),stroke_width=20,sharpness=3)
        self.play(Write(text5))
        self.add(b2)
        text6 = Tex(
                    r"$n^{n}$  \text{   ,  n为奇}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,-15,0])
        text7 = Tex(
                    r"$\dfrac{1}{n^{n}}$   \text{   ,  n为偶}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,-25,0])
        self.play(Write(text6),Write(text7))
        #C选项改变

        dot100 = Dot([16, 5, 0])
        dot200 = Dot([16, -5, 0])
        line300 = Line(dot100, dot200).set_color(ORANGE)
        text150 = Tex(
                    r"$(C)\sqrt[n] {a_{n}b_{n}}=$",color=RED_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([8,0,0])
        
        b50 = Brace(line3, direction=line1.copy().rotate(-PI / 2).get_unit_vector(),stroke_width=20,sharpness=3,color=RED_C)
        text160 = Tex(
                    r"$n$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([19,5,0])
        text170 = Tex(
                    r"$\dfrac{1}{n}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([19,-5,0])
        
        self.play(
            ReplacementTransform(text15,text150),
            ReplacementTransform(text16,text160),
            ReplacementTransform(text17,text170),
            ReplacementTransform(b5,b50),
            FadeOut(arrow),FadeOut(arrow1),
            FadeOut(text21),FadeOut(text22)

        )
        self.wait(0.5)
        #C和框框消失
        self.play(
            FadeOut(text150),FadeOut(text160),
            FadeOut(text170),FadeOut(b50),
            FadeOut(rectangle2)

        )
        #总结
        self.camera.frame.save_state()
        self.play(self.camera.frame.animate.scale(0.8).move_to([10,25,0]),run_time=2)
        self.play(self.camera.frame.animate.scale(1).move_to([-30,-20,0]),run_time=2)
        self.play(self.camera.frame.animate.scale(1).move_to([15,-20,0]),run_time=2)
        self.play(Restore(self.camera.frame))
        #红色透明框框标记
        rectangle4=Rectangle(color=RED,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-31,15,0])
        rectangle5=Rectangle(color=RED,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([12,15,0])
        rectangle6=Rectangle(color=RED,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([32,15,0])
        self.play(
            rectangle4.animate.stretch(15.0,dim=0),
            rectangle5.animate.stretch(15.0,dim=0),
            rectangle6.animate.stretch(25.0,dim=0)
        )

        #箭头+镜头拉近
        arrow = Arrow(start=[-20,7,0],end=[-15,12,0], color=PURE_RED
                       , stroke_width=20, tip_length=1)
        self.play(self.camera.frame.animate.scale(0.8).move_to([-12,15,0]),run_time=1)
        self.play(Create(arrow))










        self.wait()